package com.settlement.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.constant.SystemConstants;
import com.settlement.system.common.model.Option;
import com.settlement.system.model.entity.SysImportLog;
import com.settlement.system.mapper.SysImportLogMapper;
import com.settlement.system.model.entity.SysPlatform;
import com.settlement.system.model.entity.SysStore;
import com.settlement.system.model.entity.SysStoreAccount;
import com.settlement.system.model.query.ImportLogPageQuery;
import com.settlement.system.model.query.PlatformPageQuery;
import com.settlement.system.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-10-12
 */
@Service
@RequiredArgsConstructor
public class SysImportLogServiceImpl extends ServiceImpl<SysImportLogMapper, SysImportLog> implements SysImportLogService {

    private final SysStoreAccountService storeAccountService;
    private final SysStoreService storeService;
    private final SysDictTypeService dictTypeService;

    @Override
    public Long insertOrUpdate(SysImportLog sysImportLog) {
        SysImportLog importLog = this.baseMapper.selectOne(new LambdaQueryWrapper<SysImportLog>().eq(SysImportLog::getStoreAccountId,
                sysImportLog.getStoreAccountId()).eq(SysImportLog::getSettlementDate,
                sysImportLog.getSettlementDate()));
        if (importLog != null) {
            // 清空某些字段
            importLog.setRemark(null);
            importLog.setImportCount(0);
            this.saveOrUpdate(importLog);
            return importLog.getId();
        }
        importLog = new SysImportLog();
        importLog.setSettlementDate(sysImportLog.getSettlementDate());
        importLog.setStoreAccountId(sysImportLog.getStoreAccountId());
        importLog.setTotalCount(sysImportLog.getTotalCount());
        this.saveOrUpdate(importLog);
        return importLog.getId();
    }

    @Override
    public boolean addCount(Long logId) {
        SysImportLog importLog = this.baseMapper.selectById(logId);
        if (importLog == null) {
            return true;
        }
        // 导入数加1
        importLog.setImportCount(importLog.getImportCount() + 1);
        this.saveOrUpdate(importLog);
        return true;
    }

    @Override
    public boolean errorCount(Long logId) {
        SysImportLog importLog = this.baseMapper.selectById(logId);
        if (importLog == null) {
            return true;
        }
        // 失败数加1
        importLog.setErrorCount(importLog.getErrorCount() + 1);
        this.saveOrUpdate(importLog);
        return true;
    }

    @Override
    public Page<SysImportLog> getImportPage(ImportLogPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysImportLog> page = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysImportLog>()
                        .eq(StrUtil.isNotBlank(queryParams.getSettlementDate()), SysImportLog::getSettlementDate, queryParams.getSettlementDate())
                        .orderByDesc(SysImportLog::getUpdateTime)
        );

        List<Option> platformOptionList = dictTypeService.listDictItemsByTypeCode(SystemConstants.PLATFORM_KEY);
        List<SysStore> storeList = storeService.list();
        List<SysStoreAccount> storeAccountList = storeAccountService.list();
        for (SysImportLog record : page.getRecords()) {
            // 店铺账号
            storeAccountList.stream().filter(storeAccount -> storeAccount.getId().equals(record.getStoreAccountId())).findFirst()
                    .ifPresent(sysStoreAccount -> {
                        record.setUserName(sysStoreAccount.getUserName());
                        SysStore sysStore = storeList.stream().filter(store -> store.getCommercePlatformStoreCode().equals(sysStoreAccount.getCommercePlatformStoreCode())).findFirst().orElse(null);
                        if (sysStore != null) {
                            record.setStoreName(sysStore.getCommercePlatformStoreName());
                            platformOptionList.stream().filter(platform -> platform.getValue().equals(sysStore.getStoreKey())).findFirst().ifPresent(sysPlatform -> record.setPlatformName(sysPlatform.getLabel()));
                        }
                    });
            // 店铺
            storeList.stream().filter(sysStore -> sysStore.getId().equals(record.getStoreAccountId())).findFirst()
                    .ifPresent(sysStore -> {
                        record.setStoreName(sysStore.getCommercePlatformStoreName());
                        platformOptionList.stream().filter(platform -> platform.getValue().equals(sysStore.getStoreKey())).findFirst().ifPresent(sysPlatform -> record.setPlatformName(sysPlatform.getLabel()));
                    });
        }
        return page;
    }
}
